// pagesFilterDicSelect/pagesFilterDicSelect.js

const app = getApp()
import {
  convertToTree,
  dicToConvert,
} from '../../utils/util.js';
import {
  createStoreBindings
} from "mobx-miniprogram-bindings";
import {
  dicSelectStore
} from '../../store/dicSelectStore.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiple: true,
    // 要返回的页面路径
    backToPath: '',
    dataList: [],
    sourceData: [],
    copyViewListData: [],
    viewListData: [],
    storeKey: '',
    echoValue: [],
  },



  // 确认
  onConfirm({
    detail
  }) {
    this.confirm(detail.value, this.data.storeKey)
    wx.redirectTo({
      url: `${this.data.backToPath}`,
    })
  },
  // 取消
  onCancel() {

    wx.redirectTo({
      url: `${this.data.backToPath}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.storeBindings = createStoreBindings(this, {
      store: dicSelectStore,
      fields: ['choseDicValue'],
      actions: ["confirm"],
    });
    this.storeBindings.updateStoreBindings()
    const is = {
      'false': false,
      'true': true
    }
    const {
      multiple,
      backToPath,
      dicKey,
      storeKey
    } = options
    if (dicKey) {
      const findValue = app.globalData.dicList.find((item) => item.type === dicKey)
      const sourceData = dicToConvert(findValue.data)
      const treeList = convertToTree(sourceData, {
        id: 'id',
        parentId: 'parentId',
        name: 'dictLabel'
      })
      const copyViewListData = treeList
      const viewListData = treeList
      console.log(this.data.choseDicValue, 'choseDicValue');
      this.setData({
        multiple: is[multiple],
        backToPath: backToPath,
        sourceData: sourceData,
        copyViewListData: copyViewListData,
        viewListData: viewListData,
        storeKey: storeKey,
        echoValue: this.data.choseDicValue.map(item => item.value)

      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.storeBindings.destroyStoreBindings()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})